//
//  ViewController.m
//  上传
//
//  Created by student on 16/3/30.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#define kBoundary @"12345"
#define kNewLine @"\r\n"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self uploadFile];
}

// 1 文件相关参数
//-- ----WebKitFormBoundaryhGOAT9JAzHyJABU0
//Content-Disposition: form-data; name="file"; filename="文件名"
//Content-Type: text/plain
//
// 2.
//文件内容

//-- ----WebKitFormBoundaryhGOAT9JAzHyJABU0
// 3.
//Content-Disposition: form-data; name="username"
//
//abc
// 4.
//-- ----WebKitFormBoundaryhGOAT9JAzHyJABU0--

// Content-Type参考: http://tool.oschina.net/commons/


- (void)uploadFile
{
    //请求的网址
    NSURL *url = [NSURL URLWithString:@"http://192.168.13.108:8080/MJServer/upload"];
    
    //请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //POST
    request.HTTPMethod = @"POST";
    
    //设置请求头
    NSString *headerStr = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBoundary];
    [request setValue:headerStr forHTTPHeaderField:@"Content-Type"];
    
    
    
    //请求体
    NSMutableData *mData = [NSMutableData data];
    NSMutableString *mStr = [NSMutableString string];
    
    //文件参数
    [mStr appendString:kNewLine];
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Disposition: form-data; name=\"file\"; filename=\"2.txt\""];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Type: text/plain"];// 文件类型对应的Content-Type image/jpeg image/png
    [mStr appendString:kNewLine];
    [mStr appendString:kNewLine];
    NSLog(@"%@",mStr);
    [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    //文件的数据
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"text"];
    NSData *data = [NSData dataWithContentsOfFile:filepath];
    [mData appendData:data];
    
    //其他参数
    mStr = [NSMutableString string];
    [mStr appendString:kNewLine];
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Disposition: form-data; name=\"username\""];
    [mStr appendString:kNewLine];
    [mStr appendString:kNewLine];
    [mStr appendString:@"abc"];
    [mStr appendString:kNewLine];
    //4. 结尾
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:@"--"];
    [mStr appendString:kNewLine];
    NSLog(@"%@",mStr);
    [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    request.HTTPBody = mData;

    
    
    
    
    
    
    
    // 发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if(connectionError == nil)
        {
            NSError *error;
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
            if(error!= nil)
            {
                NSLog(@"%@",dict);
            }
            else
            {
                NSLog(@"%@",[error localizedDescription]);
            }
        }
        else
        {
            NSLog(@"%@",[connectionError localizedDescription]);
        }
    }];    
}

@end



//POST http://wu-calculator.apple.com/dgw?imei=APPLE&apptype=finance HTTP/1.1
//Host: wu-calculator.apple.com
//Proxy-Connection: keep-alive
//Accept-Encoding: gzip, deflate
//Content-Type: text/xml
//Accept-Language: en-us
//X-Client-ID: 38582892
//Accept: */*
//          Cache-Control: no-cache
//          Content-Length: 299
//          Connection: keep-alive
//          User-Agent: Search%20Framework/1.0 CFNetwork/758.3.15 Darwin/15.4.0

          
          
//POST /dgw?imei=APPLE&apptype=finance HTTP/1.1
//Host	wu-calculator.apple.com
//Accept-Encoding	gzip, deflate
//Content-Type	text/xml
//Accept-Language	en-us
//X-Client-ID	38582892
//Accept	*/*
//          Cache-Control	no-cache
//          Content-Length	299
//          Connection	keep-alive
//          User-Agent	Search%20Framework/1.0 CFNetwork/758.3.15 Darwin/15.4.0

          
